package GUILayer;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.CellRendererPane;
import javax.swing.DefaultListModel;
import javax.swing.JScrollBar;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.Spring;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.StringTokenizer;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import ControlLayer.CtrOrderLine;
import ControlLayer.CtrSalesOrder;
import javax.swing.JTable;
import org.w3c.dom.CharacterData;

public class OrderLineGUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JList list;
	private CtrSalesOrder ctrSalesOrder = new CtrSalesOrder();
	private SalesOrderGUI salesOrder = new SalesOrderGUI();;
	private DefaultListModel listModel = new DefaultListModel();
	private CtrOrderLine orderLine = new CtrOrderLine();
	private JPanel panel_3;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					try{
						 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					}catch(Exception e){
						e.printStackTrace();
					}
					OrderLineGUI frame = new OrderLineGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public OrderLineGUI() {
		setTitle("Odering process");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 514, 333);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final JPanel panel_2 = new JPanel();
		panel_2.setBounds(0, 0, 508, 305);
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(250, 36, 248, 258);
		panel_2.add(panel_1);
		panel_1.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 248, 258);
		panel_1.add(scrollPane);
		
		list = new JList(listModel);
		scrollPane.setViewportView(list);
		
		JLabel lblExistingOrderLines = new JLabel("Existing order lines:");
		lblExistingOrderLines.setBounds(250, 11, 94, 14);
		panel_2.add(lblExistingOrderLines);
		
		panel_3 = new JPanel();
		panel_3.setBounds(0, 0, 240, 305);
		panel_2.add(panel_3);
		panel_3.setLayout(null);
		
		final JPanel panel = new JPanel();
		panel.setBounds(0, 0, 240, 295);
		panel_3.add(panel);
		panel.setLayout(null);
		
		JButton btnAddNewOrder = new JButton("Add new order line");
		
		btnAddNewOrder.setBounds(10, 86, 195, 23);
		panel.add(btnAddNewOrder);
		
		JButton btnNewButton = new JButton("Cancel");
		
		btnNewButton.setBounds(10, 261, 195, 23);
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Create sales order");
		btnNewButton_1.setBounds(10, 227, 195, 23);
		panel.add(btnNewButton_1);
		
		btnNewButton_1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ev){
				double total = 0;
				panel.setVisible(false);
				panel_3.add(salesOrder);
				ctrSalesOrder.createNewSalesOrder(null, 0, null , null, null, false, 0);
				for(int i=0; i<listModel.size();i++){
					String[] result = listModel.getElementAt(i).toString().split(" x ");
					try{
						total += orderLine.searchProduct(result[1]).getSalesPrice() * Integer.parseInt(result[0]);
					}catch(Exception ex){
						ex.printStackTrace();
					}
					orderLine.addProduct(ctrSalesOrder.getLatestSaleID(), result[1] ,Integer.parseInt(result[0]));
				}
				salesOrder.setAmount(total);
			}
		});
		
		JLabel lblProductName = new JLabel("Product name:");
		lblProductName.setBounds(10, 11, 70, 14);
		panel.add(lblProductName);
		
		JLabel lblQuantity = new JLabel("Quantity");
		lblQuantity.setBounds(10, 36, 70, 14);
		panel.add(lblQuantity);
		
		textField = new JTextField();
		textField.setBounds(90, 8, 115, 20);
		panel.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(90, 33, 115, 20);
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		final JLabel label = new JLabel("");
		label.setBounds(10, 61, 195, 14);
		panel.add(label);
		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listModel.clear();
			}
		});
		btnAddNewOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try{
					String	pname = orderLine.searchProduct(textField.getText()).getPName() ;
					String quantity = textField_1.getText();
					String listData = quantity + " x " + pname;
					if(!pname.equals(null)){
					listModel.addElement(listData);
					textField.setText("");
					textField_1.setText("");
					label.setText("Item added to the order line.");
					}
				}catch(SQLException sql){
					sql.printStackTrace();
				}catch(Exception e){
					label.setText("Could not find the item.");
				}
			}
		});
	}
}